package com.shawn.appupdater;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.shawn.appupdater.updater.AppUpdater;
import com.shawn.appupdater.updater.bean.DownloadBean;
import com.shawn.appupdater.updater.net.INetCallBack;
import com.shawn.appupdater.updater.ui.UpdaterVersionShowDialog;
import com.shawn.appupdater.updater.utils.AppUtils;

public class MainActivity extends AppCompatActivity {

    // 服务器请求地址
    final String SERVER_URL = "http://59.110.162.30/app_updater_version.json";

    Button btn_app_updater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_app_updater = findViewById(R.id.btn_app_updater);

        btn_app_updater.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppUpdater.getInstance().getiNetManager().get(SERVER_URL, new INetCallBack() {
                    @Override
                    public void success(String response) {
                        /*
                        * 1.解析JSON
                        * 2.做版本匹配
                        * 3.需要升级就弹窗 用户就可以点击下载
                        * {
                             "title":"4.5.0更新啦！",
                             "content":"1. 优化了阅读体验；\n2. 上线了 hyman 的课程；\n3. 修复了一些已知问题。",
                             "url":"http://59.110.162.30/v450_imooc_updater.apk",
                             "md5":"14480fc08932105d55b9217c6d2fb90b",
                             "versionCode":"450"
                            }
                        * */
                        // 解析JSON
                        DownloadBean downloadBean = DownloadBean.parse(response);
                        if (downloadBean != null) {
                            // 版本校验
                            try {
                                // 解析服务器返回的版本号
                                long versionCode = Long.parseLong(downloadBean.getVersionCode());
                                // 获取现在的版本号
                                if (versionCode <= AppUtils.getVersionCode(MainActivity.this)) {
                                    Toast.makeText(MainActivity.this, "已经是最新版本", Toast.LENGTH_SHORT).show();
                                    return;
                                } else {
                                    UpdaterVersionShowDialog.show(MainActivity.this,downloadBean);
                                }
                            } catch (NumberFormatException e) {
                                e.printStackTrace();
                                Toast.makeText(MainActivity.this, "返回数据的版本号异常", Toast.LENGTH_SHORT).show();
                                return;
                            }


                        } else {
                            Toast.makeText(MainActivity.this, "版本更新接口返回数据异常", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void failed(Throwable throwable) {
                        Toast.makeText(MainActivity.this, "版本更新接口请求失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}
